#include "dialog.h"
#include "./ui_dialog.h"
#include <QFileDialog>
#include <QCoreApplication>
#include <QColorDialog>
#include <QFontDialog>
#include <QProgressDialog>
#include <QElapsedTimer>
#include <QInputDialog>
#include <QStringList>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
}

Dialog::~Dialog()
{
    delete ui;
}


void Dialog::on_btnOpenOneFile_clicked()
{
    QString curPath = QCoreApplication::applicationDirPath();
    QString fileName = QFileDialog::getOpenFileName(this, "选择一个文件", curPath,
                                                    "文本文件(*.txt);;图片(*.jpg *.gif *.png);;所有文件(*.*)");

    if (!fileName.isEmpty()) {
        ui->plainTextEdit->appendPlainText(fileName);
    }

}

void Dialog::on_btnOpenMultiFile_clicked()
{
    QString curPath = QCoreApplication::applicationDirPath();
    QStringList fileNames = QFileDialog::getOpenFileNames(this, "选择多个文件", curPath,
                                                          "文本文件(*.txt);;图片(*.jpg, *.gif, *.png);;所有文件(*.*)");
    if (!fileNames.isEmpty()) {
        for (auto &fileName : fileNames) {
            ui->plainTextEdit->appendPlainText(fileName);
        }
    }

}

void Dialog::on_btnChooseDir_clicked()
{
    QString curPath = QCoreApplication::applicationDirPath();
    QString selectedDir = QFileDialog::getExistingDirectory(this, "选择一个目录", curPath);
    if (!selectedDir.isEmpty()) {
        ui->plainTextEdit->appendPlainText(selectedDir);
    }
}

void Dialog::on_btnSaveFile_clicked()
{
    QString curPath = QCoreApplication::applicationDirPath();
    QString fileName = QFileDialog::getSaveFileName(this, "保存文件", curPath,
                                                    "文本文件(*.txt);;图片(*.jpg, *.gif, *.png);;所有文件(*.*)");
    if (!fileName.isEmpty()) {
        ui->plainTextEdit->appendPlainText(fileName);
    }
}


void Dialog::on_btnChooseColor_clicked()
{
    QPalette plet = ui->plainTextEdit->palette();
    QColor iniColor = plet.color(QPalette::Text);
    QColor color = QColorDialog::getColor(iniColor, this, "选择颜色");
    if (color.isValid()) {
        plet.setColor(QPalette::Text, color);
        ui->plainTextEdit->setPalette(plet);
    }
}


void Dialog::on_btnChooseFont_clicked()
{
    QFont iniFont = ui->plainTextEdit->font();
    bool ok(false);
    QFont font = QFontDialog::getFont(&ok, iniFont);
    if (ok) {
        ui->plainTextEdit->setFont(font);
    }
}

void Dialog::on_btnProgressDialog_clicked()
{
    const int minValue = 0;
    const int maxValue = 200;
    QProgressDialog dlgProgress("正在复制文件...", "取消", minValue, maxValue, this);
    dlgProgress.setWindowTitle("复制文件");
    dlgProgress.setWindowModality(Qt::WindowModal);

    connect(&dlgProgress, &QProgressDialog::canceled, this, [&](){ui->plainTextEdit->appendPlainText("进度已取消!");});

    QElapsedTimer msCounter;
    for (int i = minValue; i <= maxValue; ++i) {
        dlgProgress.setValue(i);
        dlgProgress.setLabelText(QString::asprintf("正在复制文件, 第%d个", i));

        msCounter.start();
        while (true) {
            if (msCounter.elapsed() > 30) { // 每次30ms跳出循环
                break;
            }
        }

        if (dlgProgress.wasCanceled()) { // 如果有人点了取消按钮
            break;
        }
    }
}

void Dialog::on_btnInputString_clicked()
{
    QString dlgTitle = "输入文字对话框";
    QString txtLabel = "请输入文件名";
    QString iniInput = "新建文件.txt";
    bool ok(false);

    QLineEdit::EchoMode echo = QLineEdit::Normal;
    QString text = QInputDialog::getText(this, dlgTitle, txtLabel, echo, iniInput, &ok);
    if (ok && !text.isEmpty()) {
        ui->plainTextEdit->appendPlainText(text);
    }
}


void Dialog::on_btnInputInt_clicked()
{
    QString dlgTitle = "输入整数对话框";
    QString txtLabel = "设置文本框字体大小";
    int defaultValue = ui->plainTextEdit->font().pointSize();
    int minValue = 6;
    int maxValue = 60;
    int stepValue = 1;
    bool ok(false);

    int inputValue = QInputDialog::getInt(this, dlgTitle, txtLabel, defaultValue, minValue, maxValue, stepValue, &ok);
    if (ok) {
        QString str = QString("文本框字体大小被设置为: %1").arg(inputValue);
        ui->plainTextEdit->appendPlainText(str);
        QFont font = ui->plainTextEdit->font();
        font.setPointSize(inputValue);
        ui->plainTextEdit->setFont(font);
    }
}


void Dialog::on_btnInputFloat_clicked()
{
    QString dlgTitle = "输入浮点数对话框";
    QString txtLabel = "设置文本框字体大小";
    float defaultValue = 3.13;

    float minValue = 0;
    float maxValue = 10000;
    int decimals = 2;

    bool ok(false);

    float inputValue = QInputDialog::getDouble(this, dlgTitle, txtLabel, defaultValue, minValue, maxValue, decimals, &ok);
    if (ok) {
        QString str = QString::asprintf("输入了一个浮点数: %.2lf", inputValue);
        ui->plainTextEdit->appendPlainText(str);
    }
}

void Dialog::on_btnChooseItem_clicked()
{
    QStringList items;
    items << "优" << "良" << "中" << "差";

    QString dlgTitle = "输入项对话框";
    QString txtLabel = "请选择一个级别";
    int curIndex = 0;
    bool editable(false);
    bool ok(false);

    QString text = QInputDialog::getItem(this, dlgTitle, txtLabel,items, curIndex, editable, &ok);
    if (ok && !text.isEmpty()) {
        ui->plainTextEdit->appendPlainText(text);
    }
}

#include <QMessageBox>

void Dialog::on_btnQuestion_clicked()
{
    QString dlgTitle= "Question 消息框";
    QString strInfo = "文件已被修改, 是否保存?";

    int res = QMessageBox::question(this, dlgTitle, strInfo,
                          QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
                          QMessageBox::NoButton);
    QString str;
    switch(static_cast<QMessageBox::StandardButton>(res)) {
    case QMessageBox::Yes:
        str = "Question消息框: Yes被选中";
        break;
    case QMessageBox::No:
        str = "Question消息框: No被选中";
        break;
    case QMessageBox::Cancel:
        str = "Question消息框: Cancel被选中";
        break;
    default:
        str = "Question消息框: 没有按钮被选中";
    }
    ui->plainTextEdit->appendPlainText(str);
}


void Dialog::on_btnInformation_clicked()
{
    QString dlgTitle= "Information 消息框";
    QString strInfo = "文件已打开, 请检查";

    QMessageBox::information(this, dlgTitle, strInfo);
}

void Dialog::on_btnWarning_clicked()
{
    QString dlgTitle= "Warning 消息框";
    QString strInfo = "文件已被修改!";

    QMessageBox::warning(this, dlgTitle, strInfo);
}

void Dialog::on_btnCritical_clicked()
{
    QString dlgTitle= "Warning 消息框";
    QString strInfo = "文件已被修改!!!";

    QMessageBox::critical(this, dlgTitle, strInfo);
}


void Dialog::on_btnAbout_clicked()
{
    QString dlgTitle= "About 消息框";
    QString strInfo = "xxxx版本1.0!";

    QMessageBox::about(this, dlgTitle, strInfo);
}


void Dialog::on_btnAboutQt_clicked()
{
    QString dlgTitle = "aboutQt 消息框";
    QMessageBox::aboutQt(this, dlgTitle);
}


void Dialog::on_btnClearText_clicked()
{
    ui->plainTextEdit->clear();
}


void Dialog::on_btnExit_clicked()
{
    close();
}

